﻿Imports System.Xml
Imports System.Data
Imports System.Web
Imports System.Text
Imports Mysoft.Map.Data
Imports Mysoft.Map.Data.MyDB
Imports System.Data.SqlClient

Public Class Cbgl2Xmjd

    '功能：根据工作项GUID显示工作项目名称
    Public Shared Function ShowWorkName(ByVal strWorkGUID As String) As String
        Dim strWorkName As String
        Dim dtTemp As DataTable
        Dim strSQL As String

        strSQL = "SELECT WorkShortName FROM jd_work WHERE WorkGUID='" + strWorkGUID + "'"
        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count > 0 Then
            strWorkName = dtTemp.Rows(0).Item("WorkShortName")
        Else
            strWorkName = ""
        End If

        Return strWorkName
    End Function

    '功能：插入工作项对应业务数据
    Public Shared Function InsertWorkItemRelation(ByVal strWorkGUID As String, ByVal strRefGUID As String, ByVal strType As String) As Boolean
        Dim strSQL As String

        strSQL = "INSERT jd_work2Business(WorkGUID,BusinessGUID,Type) " & _
                 " VALUES('" & strWorkGUID & "','" & strRefGUID & "','" & strType & "')"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return False
        Else
            Return True
        End If
    End Function


    '功能：插入工作项对应业务数据
    Public Shared Function CreateWorkItemRelation(ByVal strWorkGUIDList As String, ByVal strRefGUID As String, ByVal strType As String) As Boolean
        Dim strSQL As String

        strSQL = "INSERT INTO jd_Work2Business(Work2BusinessGUID,WorkGUID,BusinessGUID,ConfirmedBeginDate,Type)" & _
                " SELECT Work2BusinessGUID,WorkGUID,BusinessGUID,ConfirmedBeginDate,Type " & _
                " FROM(SELECT newid() AS Work2BusinessGUID,WorkGUID,@RefGUID AS BusinessGUID,BeginDateJh AS ConfirmedBeginDate,'" & strType & "' as Type" & _
                 "       FROM jd_Work WHERE CHARINDEX(CONVERT(varchar(40),jd_Work.WorkGUID)+',',@strWorkGuidList+',')>0 " & _
                 ") a WHERE NOT EXISTS( SELECT 8 FROM jd_Work2Business b WHERE b.WorkGUID = a.WorkGUID)"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return False
        Else
            Return True
        End If
    End Function


    '功能：业务确认后更新工作项对应业务表确认后计划时间
    Public Shared Function UpdateWorkItemRelation(ByVal strWorkGUID As String, ByVal strRefGUID As String, ByVal strType As String) As Boolean
        Dim strSQL As String

        strSQL = "UPDATE jd_work2Business" & _
                 " SET jd_work2Business.ConfirmedBeginDate = b.BeginDateJh" & _
                 " FROM vjd_work2Business b" & _
                 " WHERE jd_work2Business.Work2BusinessGUID = b.Work2BusinessGUID" & _
                 " AND jd_work2Business.WorkGUID = '" & strWorkGUID & "'" & _
                 " AND jd_work2Business.BusinessGUID = '" & strRefGUID & "'" & _
                 " AND jd_work2Business.Type = '" & strType & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    '功能：业务确认后更新工作项对应业务表确认后计划时间
    Public Shared Function UpdateWorkItemRelation(ByVal strRefGUID As String, ByVal strType As String) As Boolean
        Dim strSQL As String

        strSQL = "UPDATE jd_work2Business" & _
                 " SET jd_work2Business.ConfirmedBeginDate = b.BeginDateJh" & _
                 " FROM vjd_work2Business b" & _
                 " WHERE jd_work2Business.Work2BusinessGUID = b.Work2BusinessGUID" & _
                 " AND jd_work2Business.BusinessGUID = '" & strRefGUID & "'" & _
                 " AND jd_work2Business.Type = '" & strType & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    '功能：删除工作项删除，删除工作项对应业务表数据
    Public Shared Function DeleteWorkItemRelation(ByVal strWorkGUID As String) As Boolean
        Dim strSQL As String

        strSQL = "DELETE jd_Work2Business WHERE WorkGUID='" & strWorkGUID & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    '功能：取消合同、合约规划工作项选择，删除工作项对应业务表数据
    Public Shared Function DeleteWorkItemRelation(ByVal strRefGUID As String, ByVal strType As String) As Boolean
        Dim strSQL As String

        strSQL = "DELETE jd_Work2Business WHERE BusinessGUID='" & strRefGUID & "' AND Type = '" & strType & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    '功能：取消合同、合约规划工作项选择，删除工作项对应业务表数据
    Public Shared Function DeleteWorkItemRelation(ByVal strWorkGUID As String, ByVal strRefGUID As String, ByVal strType As String) As Boolean
        Dim strSQL As String

        strSQL = "DELETE jd_Work2Business WHERE WorkGUID='" & strWorkGUID & "' AND BusinessGUID='" & strRefGUID & "' AND Type = '" & strType & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    '功能：判断合同、合约规划对应工作项的时间是否有调整
    Public Shared Function WorkItemIfChange(ByVal strWorkGUID As String, ByVal strRefGUID As String) As Boolean
        Dim strSQL As String

        strSQL = "SELECT WorkGUID FROM vjd_Work2Business WHERE BusinessGUID='" & strRefGUID & "' AND Convert(varchar(10),ConfirmedBeginDate)!=Convert(varchar(10),BeginDateJh)"

        If MyDB.GetDataTable(strSQL).Rows.Count > 0 Then
            Return False
        Else
            Return True
        End If
    End Function

End Class
